﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;

namespace GiaoDien
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack != true)
            {
                int SoLuongMua = 1;
                if(Request.QueryString["SoLuongMua"] != null)
                {
                   SoLuongMua = Int32.Parse(Request.QueryString["SoLuongMua"].ToString());
                }
                if (Session["DangNhap"] == null)
                {
                    Response.Redirect("ListSanPham.aspx");
                }
                // Lấy giỏ hàng
                List<ServiceKD.SanPham> lst = new List<GiaoDien.ServiceKD.SanPham>();
                if (Session["GioHang"] != null)
                {
                    lst = (List<ServiceKD.SanPham>)Session["GioHang"];
                }

                ServiceKD.ServiceSoapClient proxy = new GiaoDien.ServiceKD.ServiceSoapClient("ServiceSoap");
                string action = null;
                if (Request.QueryString["action"] != null)
                    action = Request.QueryString["action"].ToString();
                //Thêm sản phẩm vào giỏ hàng
                if (action == "add")
                {
                    int MaSP = 0;
                    if (Request.QueryString["MaSP"] != null)
                        MaSP = Int32.Parse(Request.QueryString["MaSP"].ToString());

                    ServiceKD.SanPham temp = proxy.layThongTinSanPham(MaSP);
                    temp.soLuong = SoLuongMua;

                    Boolean flag = false;
                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (MaSP == lst[i].maSanPham)
                        {
                            flag = true;
                            lst[i].soLuong += SoLuongMua;
                        }
                    }
                    if (flag == false)
                    {
                        lst.Add(temp);
                    }
                    Session["GioHang"] = lst;
                }
                //Xóa tất cả sản phẩm khỏi giỏ hàng
                if (action == "deleteall")
                {
                    lst = new List<GiaoDien.ServiceKD.SanPham>();
                    Session["GioHang"] = lst;
                }


                GrGioHang.DataSource = lst;
                GrGioHang.DataBind();
                double s = 0;
                for (int i = 0; i < lst.Count; i++)
                {
                    s += (Double)(lst[i].soLuong * lst[i].giaBanQuiDinh);
                }
                lblTongTien.Text = s + "";


            }

        }

        protected void btnTiepTucMuaHang_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListSanPham.aspx");
        }

        protected void btnThanhToan_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThanhToan.aspx");
        }

        protected void btnXoaTatCa_Click(object sender, EventArgs e)
        {
            Response.Redirect("GioHang.aspx?action=deleteall");
        }
    }
}
